package com.lmm.lcbusiness.lcbusiness.pages.rnpages.plugins;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
import com.lmm.dresswisdom.lmmframe.reactnative.BasePlugin;
import com.lmm.dresswisdom.lmmframe.reactnative.annotation.RNPlugin;
import com.lmm.lcbusiness.lcbusiness.dbmodel.ItemRealm;
import com.lmm.lcbusiness.lcbusiness.pages.rnpages.ReactNativeActivity;
import com.lmm.lcbusiness.lcbusiness.utils.RealmUtils;

import java.util.Map;

import io.realm.Realm;
import io.realm.RealmResults;

/**
 * Created by xmgong on 2017/12/15.
 */
@RNPlugin(method = "getitemvalue")
public class GetItemValuePlugin extends BasePlugin<ReactNativeActivity> {
    public GetItemValuePlugin(ReactNativeActivity act) {
        super(act);
    }

    @Override
    protected void doAction(Map<String, Object> map, Callback callback) {
        if (map.containsKey("dicid")) {
            int dicid = (int) map.get("dicid");

            Realm realm = RealmUtils.getRealm();
            RealmResults<ItemRealm> results = realm.where(ItemRealm.class).equalTo("dicId", dicid).findAll();

            WritableArray array = Arguments.createArray();
            WritableMap it = null;
            for (ItemRealm item : results) {
                it = Arguments.createMap();
                it.putInt("id", item.getId());
                it.putInt("dicId", item.getDicId());
                it.putString("itemValue", item.getItemValue());
                it.putInt("sortNo", item.getSortNo());
                array.pushMap(it);
            }
            callback.invoke(array);
        }
    }


}
